#!/bin/bash
##############################################################
# File Name：tomcat.sh
# Version :V1 . 
# Author: wangXiaoFeng 
# Email: 1289832464@qq.com
# Created Time ：2023-02-07 15:43:04
# Description：
#############################################################

#Please specify the tomcat binHome directory 
tomcatBinPath='/opt/app/apache-tomcat-7.0.99/bin/'
#monitr_cronTab  
monitr_cronTab='*/1 * * * *'


curDate=`date +"%F %T"`
tomcatPid=`ps -ef|grep -v grep |grep "${tomcatBinPath}"|awk '{print $2}'`


echo -e "\n[Info]:curDate:${curDate}"
#checking tomcat
if [ -z "${tomcatPid}" ];then
   echo -e "[Error]:tomcat is not runing,\n[Info]:Begin start tomcat......"
   cd ${tomcatBinPath}
   ./startup.sh 1>/dev/null 2>&1
   sleep 3
   newtomcatPid=`ps -ef|grep -v grep |grep "${tomcatBinPath}"|awk '{print $2}'` 
   if [ -z "${newtomcatPid}" ];then
      echo -e "[Error]:Tomcat startup failed" 
   else
      echo -e "[Info]:Tomcat started successfully,Pid is:${newtomcatPid}"
   fi
else
   echo -e "[Info]:Tomcat is runing,Pid is:${tomcatPid}"
fi
echo -e "\n"

#add crontab
curPath=$(cd `dirname $0`; pwd)
fileName=`echo "$0"|awk -F/ '{print $NF}'`
touch /var/spool/cron/root
sed -i "/${fileName}/d" /var/spool/cron/root
echo "${monitr_cronTab} ${curPath}/${fileName} >> /tmp/${fileName}".log >> /var/spool/cron/root
/sbin/service crond restart $1>/dev/null 2>&1
